const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  Teacher,
} = require('../db/model');

class TeacherService extends Service {
  async getAllTeacher() {
    const res = await Teacher.findAll().catch(err => {
      console.log(`Teacher 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`Teacher 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getTeacher(teacher) {
    if (!teacher?.tId) {
      console.log(`Teacher 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${teacher}`, teacher);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    const res = await Teacher.findOne({
      where: {
        tId: teacher?.tId || -1
      },
    }).catch(err => {
      console.log(`Teacher 表查找失败: ${err}\n参数: ${teacher}`, teacher);
    });
    if (res === undefined) {
      console.log(`Teacher 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${teacher}`, teacher);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`Teacher 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${teacher}`, teacher);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendTeacher(teacher) {
    let res = await this.getTeacher(teacher);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`Teacher 表新增失败: ${res}\n参数: ${teacher}`, teacher);
      return res;
    }
    res = await Teacher.create(teacher).catch(err => {
      console.log(`Teacher 表新增失败: ${err}\n参数: ${teacher}`, teacher);
    });
    if (res === undefined) {
      console.log(`Teacher 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${teacher}`, teacher);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateTeacher(teacher) {
    let res = await this.getTeacher(teacher);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Teacher 表更新失败: ${res}\n参数: ${teacher}`, teacher);
      return res;
    }
    res = await Teacher.update(teacher, {
      where: {
        tId: teacher?.tId || -1
      },
    }).catch(err => {
      console.log(`Teacher 表更新失败: ${err}\n参数: ${teacher}`, teacher);
    });
    if (res === undefined) {
      console.log(`Teacher 表更新失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${teacher}`, teacher);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyTeacher(teacher) {
    let res = await this.getTeacher(teacher);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Teacher 表删除失败: ${res}\n参数: ${teacher}`, teacher);
      return res;
    }
    res = await Teacher.destroy({
      where: {
        tId: teacher?.tId || -1
      },
    }).catch(err => {
      console.log(`Teacher 表删除失败: ${err}\n参数: ${teacher}`, teacher);
    });
    if (res === undefined) {
      console.log(`Teacher 表删除失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${teacher}`, teacher);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = TeacherService;
